Public Class frmNewAmodSolution
    'form for creating new aMod Solution

#Region "Attributes"
#Region "Private attributes"
    Private MbReadyForUse As Boolean = False 'indicates proper creation or first initialization of form
    Private MoDfrmNewAmodSolution As ClsDfrmNewAmodSolution = Nothing 'control object for this form
    Private MoAction As ClsAction = New ClsAction(GeAction.kkNoAction) 'action for this form
    Private MeResult As GeActionResult = GeActionResult.kkOk  'result of this form
    Private MsaModSolutionName As String = "" 'aModSolution name
    Private MsaModModulesDirectoryName As String = "" 'modules directory name
    Private MsaModSolutionPath As String = "" 'aModSolution path
    Private MsaModSolutionFileDirectory As String = "" 'aModSolution file direcoty
#End Region
#End Region
#Region "Constructors & Init"
    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.


        '**********************************************************************
        InitControl()
        '**********************************************************************
    End Sub
    'setting form settings before its using
    Private Function InitControl() As GeActionResult
        SetReadyForUse(False) 'disabling form
        Try
            StartPosition = FormStartPosition.CenterParent
            WindowState = FormWindowState.Normal
            Size = New Size(439, 233)
            ShowInTaskbar = False
            ShowIcon = True
            Icon = ConvertImageToIcon(imlFormImages.Images("FORMICON"))
            Text = "Vytvorenie noveho aMod projektu"

            AcceptButton = btnOK
            CancelButton = btnClose

            SetReadyForUse(True) 'enabling form
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "Properties"
#Region "Public properties"
#End Region
#End Region
#Region "Events handler"
    Private Sub frmNewAmodSolution_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'user closed via the forms control box close button (X) or via Me.Close - I set Me.DialogResult to Windows.Forms.DialogResult.OK when leaving form in successful situation
        If e.CloseReason = System.Windows.Forms.CloseReason.UserClosing AndAlso Me.DialogResult = Windows.Forms.DialogResult.Cancel Then
            MeResult = GeActionResult.kkCancel
        End If
        'since sometimes form is going to close and I dont know why, here is place for fixing the problem :-)
        Select Case e.CloseReason
            Case CloseReason.None
                e.Cancel = True
        End Select
    End Sub
    Private Sub edtaModSolutionName_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles edtaModSolutionName.EditValueChanged
        SetEnable()
    End Sub
    Private Sub btnaModSolutionPath_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaModSolutionPath.EditValueChanged
        SetEnable()
    End Sub
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim LsaModSolutionName As String = edtaModSolutionName.Text
        Dim LsaModSolutionFileDirectory As String = btnaModSolutionPath.Text
        Dim LsaModSolutionFileName As String = LsaModSolutionName & ".aModProj"
        Dim LsaModSolutionFilePath As String = IO.Path.Combine(LsaModSolutionFileDirectory, LsaModSolutionFileName)
        Dim LsaModSolutionDirectoryPath As String = IO.Path.Combine(LsaModSolutionFileDirectory, LsaModSolutionName)

        Try
            If TryToCreateFile(LsaModSolutionFilePath) = GeActionResult.kkOk Then
                If TryToCreateFolder(LsaModSolutionDirectoryPath, False) = GeActionResult.kkOk Then
                    MsaModSolutionName = LsaModSolutionName
                    MsaModModulesDirectoryName = edtModulesDirectoryName.Text
                    MsaModSolutionPath = LsaModSolutionFilePath
                    MsaModSolutionFileDirectory = LsaModSolutionFileDirectory
                    MeResult = GeActionResult.kkOk
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()
                Else
                    'file was created and folder wasnt created --> try to delete file
                    TryToDeleteFile(LsaModSolutionFilePath)
                End If
            End If
        Catch ex As Exception
            LogEvent("Projektovy subor s projektovym adresarom sa nepodarilo vytvorit! Zadajte prosim ine umiestnenie projektu.")
            MsaModSolutionName = ""
            MsaModSolutionPath = ""
            MsaModSolutionFileDirectory = ""
            MeResult = GeActionResult.kkError
        End Try
    End Sub
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        MeResult = GeActionResult.kkCancel
        Me.Close()
    End Sub
    Private Sub btnaModSolutionPath_ButtonPressed(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles btnaModSolutionPath.ButtonPressed
        FolderBrowserDialog.SelectedPath = GetDesktopPath()
        FolderBrowserDialog.Description = "Ulozit projekt aMod"
        If FolderBrowserDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            btnaModSolutionPath.Text = FolderBrowserDialog.SelectedPath
        End If
    End Sub
    Private Sub frmNewaModSolution_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        edtaModSolutionName.SelectAll()
        edtaModSolutionName.Focus()
    End Sub
#End Region
#Region "Functions"
    Private Function CorrectData() As Boolean
        If edtaModSolutionName.Text <> "" AndAlso btnaModSolutionPath.Text <> "" Then
            Return True
        Else
            Return False
        End If
    End Function
#End Region
#Region "Subs"
    'set enable/disable for controls
    Private Sub SetEnable()
        btnOK.Enabled = CorrectData()
    End Sub
    'sets MbReadyForUse attribute and sets Enable property of form
    Private Sub SetReadyForUse(ByVal IbValue As Boolean)
        MbReadyForUse = IbValue
        Me.Enabled = IbValue 'enabling form
    End Sub
#End Region

    '#######################################

    '#######################################
    'execute function
    Public Function Execute(ByRef OoAction As ClsAction) As GeActionResult
        Try
            MoAction = OoAction
            '---------------------------------------------
            'ACTION BEFORE ACTION
            '---------------------------------------------

            '---------------------------------------------
            'ACTION
            '---------------------------------------------
            Select Case MoAction.Action
                Case GeAction.kkaModSolutionNewSolution
                    MoDfrmNewAmodSolution = MoAction.GetData("CLSDFRMNEWAMODSOLUTION")

                    edtaModSolutionName.Text = "Novy projekt"
                    edtModulesDirectoryName.Text = "Modules"
                    btnaModSolutionPath.Text = GetDesktopPath()
                    SetEnable()

                    Me.ShowDialog()

                    If MeResult = GeActionResult.kkOk Then
                        MoAction.SetData(MsaModSolutionName, "SOLUTIONNAME")
                        MoAction.SetData(MsaModModulesDirectoryName, "MODULESDIRECTORYNAME")
                        MoAction.SetData(MsaModSolutionPath, "SOLUTIONPATH")
                        MoAction.SetData(MsaModSolutionFileDirectory, "SOLUTIONFILEDIRECTORY")
                    End If
            End Select
            '---------------------------------------------
            'ACTION AFTER ACTION
            '---------------------------------------------

            '---------------------------------------------
            Return MeResult
            '---------------------------------------------

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    '#######################################

End Class